package com.bookmanagement.controller.user;

import com.bookmanagement.DTO.PasswordUpdateDTO;
import com.bookmanagement.DTO.UserAccountDTO;
import com.bookmanagement.DTO.UserUpdateDTO;
import com.bookmanagement.VO.UserAccountVO;
import com.bookmanagement.VO.UserVO;
import com.bookmanagement.constant.MessageConstant;
import com.bookmanagement.entity.User;
import com.bookmanagement.properties.JwtProperties;
import com.bookmanagement.result.Result;
import com.bookmanagement.service.UserService;
import com.bookmanagement.utils.AliOssUtil;
import com.bookmanagement.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author skyjay
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/login")
    public Result<UserAccountVO> login(@RequestBody UserAccountDTO userAccountDTO) {
        User user = userService.login(userAccountDTO);

        String token = JwtUtil.createToken(jwtProperties.getUserSecretKey()
                , jwtProperties.getUserTtl()
                , userAccountDTO.getEmail());

        UserAccountVO userAccountVO = UserAccountVO
                .builder()
                .token(token)
                .isAdmin(user.getIsAdmin())
                .status(user.getStatus())
                .build();
        return Result.success(userAccountVO);
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserAccountDTO userAccountDTO){
        userService.register(userAccountDTO);
        return Result.success();
    }

    @GetMapping("/info")
    public Result<UserVO> info(){
        UserVO userVO = userService.getInfo();
        return Result.success(userVO);
    }

    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody UserUpdateDTO userUpdateDTO){
        userService.updateUserInfo(userUpdateDTO);
        return Result.success();
    }

    @PutMapping("/password")
    public Result updatePassword(@RequestBody PasswordUpdateDTO passwordUpdateDTO){
        userService.updatePassword(passwordUpdateDTO);
        return Result.success();
    }

    /**
     * 上传头像
     */
    @PostMapping("/avatar")
    public Result<String> uploadAvatar(MultipartFile file) {
        log.info("上传头像：{}", file);
        try {
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String filename = UUID.randomUUID() + extension;
            String avatarUrl = aliOssUtil.upload(file.getBytes(), filename);
            
            // 更新用户头像
            UserUpdateDTO userUpdateDTO = new UserUpdateDTO();
            userUpdateDTO.setAvatar(avatarUrl);
            userService.updateUserInfo(userUpdateDTO);
            
            return Result.success(avatarUrl);
        } catch (IOException e) {
            log.error("头像上传失败：{}", e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
